<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class menu_model extends CI_Model{
    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    //--------------
    function getMenuParent($menuId,$status)
    {        
        $this->db->select('*');
        $this->db->from('sy_menu');
        $this->db->where('parent',$menuId);
        
        if($status != null){
            $this->db->where('status',$status);
        }else{
            $this->db->where('status !=',-1);
        }
        
        $this->db->order_by("display_order", "asc");
        $this->db->order_by("id", "asc");
        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;   
    }

    function getMenuById($id,$isobject=true){
        $query = $this->db->get_where('sy_menu', array('id' => $id));
        if($query->num_rows() == 1) {
            if($isobject == true)
                return $query->row();
            else
                return $query->row_array();
        }
        return false;
    }
    
    function insertMenu($data)
    {
        $this->db->insert('sy_menu', $data);
        return $this->db->insert_id();
    }
    
    function updateMenu($id,$data){
        $this->db->where('id', $id);
        $this->db->update('sy_menu', $data); 
    }
    
    function htmlShowOptionMenu($parent = 0,$str ='',$string ='',$id=0,$menu_parent=0)
    {
        $query = $this->getMenuParent($parent,null);
        if($query == false)
        {
            return $string;
        }
        else
        {
            foreach($query as $row) {
                if(($row->id == $id || $row->parent == $id || $row->dirparent == $id) && $id !=0)
                $string .= '';
                //$string .= '<option disabled="disabled" value="'.$row->id.'">'.$str.$row->title.'</option>';
                else 
                {
                    if($row->id == $menu_parent) $string .= '<option selected="selected" value="'.$row->id.'">'.$str.$row->title.'</option>';
                    else $string .= '<option value="'.$row->id.'">'.$str.$row->title.'</option>';
                }
                $string .= $this->htmlShowOptionMenu($row->id,$str.' - - ','',$id,$menu_parent);
            }
        }
        return $string;
    }
    
    function updateMenuParent($parent = 0,$dirparent_up)
    {
        $query = $this->getMenuParent($parent);
        if($query == false)
        {
            return false;
        }
        else
        {
            foreach($query as $row) {
                $dataupdate = array('dirparent'=>$dirparent_up);
                $this->updateMenu($row->id,$dataupdate);
                $this->updateMenuParent($row->id,$dirparent_up);
            }
        }
        return true;
    }
    
    //xoa
    function deleteMenu($menu_id)
    {
        $this->db->delete('site_menu', array('id' => $menu_id));
    }
    
    function loadMenuController()
    {
        $query = $this->getAllSiteMenu();
        $menu_data = array();
        foreach($query as $row) {
            $menu = array();
            $menu['name'] = $row->title;
            $query_par = $this->getAllSiteMenuParent($row->id);
            if($query_par){
                $menu_parent = array();
                foreach($query_par as $item) {
                    $menu_parent[] = array(
                        'name'=> $item->title,
                        'link' => $item->link_backend,
                        'is_link' => $item->is_link
                    );
                }
                $menu['parent'] = $menu_parent;
            }else{
                $menu['parent'] = null;
            }
            $menu_data[] = $menu;
        }
        return $menu_data;
    }
    
    function getAllSiteMenu(){
        $this->db->select('*');
        $this->db->from('sy_site_menu');
        $this->db->where('parent',0);
        $query = $this->db->get();

        if($query->num_rows() > 0) return $query->result();
        return false;
    }
    
    function getAllSiteMenuParent($parent){
        $this->db->select('*');
        $this->db->from('sy_site_menu');
        $this->db->where('parent',$parent);
        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;   
    }
    /*function menuParent($menuId = 0,$status = null)
    {//load menu da cap, $menuId = 0 => lay tat ca menu, != lay tat ca menu con cua menu do 
        $menu_parent = array();
        $menu = array();
        $query = $this->getMenuParent($menuId,$status);
        if($query == false){
            return null;
        }else{
            foreach($query as $row) {
                $arr = array();
                $menu_parent['id'] = $row->id;
                $menu_parent['name'] = $row->title;
                $menu_parent['parameters'] = $row->parameters;
                $menu_parent['link'] = $row->link;
                $menu_parent['order'] = $row->display_order;
                $menu_parent['status'] = $row->status;
                $menu_parent['parent'] = $this->menuParent($row->id,$status);
                $menu[] = $menu_parent;
            }
        }
        return $menu;
    }*/
    //==============
    
    

    
    function get_controller_id($id)
    {
        $query = $this->db->get_where('site_controller', array('id' => $id));
        if($query->num_rows() == 1) return $query->row();
        return false;
    }
    
    function linkFrontCoId($id)
    {
        $query = $this->get_controller_id($id);
        if($query)
        {
            return $query->link_frontend;
        }
        return '';
    }
    
    
    
    /*
    function loadMenuController()
    {//load cac chuc nang tren thanh menu
        $query = $this->get_controller();
        $string = '';
        foreach($query as $row) {
            $string .= '<li><a href="'.$row->link_backend.'">'.$row->title.'</a>';
            $string .= '<ul>';
            
            $query_par = $this->controller_parent($row->id);
            foreach($query_par as $item) {
                $string .= '<li><a href="'.$item->link_backend.'">'.$item->title.'</a></li>';
            }
            $string .= '</ul></li>';
        }

        return $string;
    }*/
    
    function getmenu_parent($parent)
    {
        $sql = "SELECT * FROM sy_menu WHERE parent = ? ORDER BY stt ASC";
        $query = $this->db->query($sql, array($parent));
        if($query->num_rows() > 0) return $query->result();
        return false;   
    }
    function load_mainmenu_dacap()
    {
        $string = $this->showmenu(0,'','');
        return $string;
    }

    function showmenu($parent = 0,$string ='',$str='')
    {
        $query = $this->getmenu_parent($parent);
        if($query == false)
        {
            return $string;
        }
        else
        {
            $string .= '<ul>';
            foreach($query as $row) {
                $string .= '<tr>';
                $string .= '<td>'.$str.'<a href="'.site_url("option=menu&view=menuDetails&id=".$row->id).'">'.$row->title.'</a></td>';
                $string .= '<th>'.$row->stt.'</th>';
                if($row->published)
                    $string .= '<th align="center"><img width="16" height="16" border="0" alt="" src="template/img/tick.png"></th>';
                else
                    $string .= '<th align="center"><img width="16" height="16" border="0" alt="" src="template/img/publish.png"></th>';
                $string .= '<th>'.$row->id.'</th>';
                $string .= '</tr>';
                $string .= $this->showmenu($row->id,'',$str.'* - - -');
            }
            $string .= '</ul>';
        }
        return $string;
    }
    
    /*function updateMenu($id,$data)
    {
        $this->db->where('id', $id);
        $this->db->update('site_menu', $data); 
    }*/
    
    function update_menu_parent_delete($parent = 0)
    {
        $query = $this->getmenu_parent($parent);
        if($query == false)
        {
            return false;
        }
        else
        {
            foreach($query as $row) {
                $dataupdate = array('parent' => 0,'dirparent'=>0);
                $this->updateMenu($row->id,$dataupdate);
                $this->update_menu_parent_delete($row->id);
            }
        }
        return true;
    }
}
?>
